/*
 * Created by Icodeon Ltd.
 * Date: 18/01/2007
 * 
 * Copyright 2006 All Rights Reserved
 * Icodeon Ltd 
 * St Johns Innovation Centre
 * Cowley Road
 * Cambridge 
 * CB4 0WS England
 * 
 * Registered in England and Wales No: 5068195
 * Use is subject to license terms.
 */

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Web;
using System.Text;

namespace Icodeon.Xml
{
    public sealed class Serializer
    {
        /// <summary>
        /// Serializes a instance of a class type to an XML document instance
        /// </summary>
        /// <param name="instance">The class to be serialized</param>
        /// <param name="file">The file</param>
        /// <param name="defaultNameSpace">The default namespace for the XML file</param>
        public void SerializeToFile(Object instance, FileInfo file, string defaultNameSpace)
        {
            /*
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
             */

            XmlTextWriter xmlTextWriter = null;
            Exception rethrow = null;
            try
            {
                /*
                 * Use the new UTF8Encoding(false, false) to ensure that the the Byte Order Mark is not in the prolog
                 * 
                 * Initializes a new instance of the UTF8Encoding class. Parameters specify whether 
                 * to provide a Unicode byte order mark and whether to throw an exception when an invalid encoding is detected. 
                 * 
                 * See http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.utf8encoding.aspx
                 */

                XmlSerializer xmlSerializer = new XmlSerializer(instance.GetType(), defaultNameSpace);
                bool encoderShouldEmitUTF8Identifier = false;
                bool throwOnInvalidBytes = true;

                xmlTextWriter = new XmlTextWriter(file.FullName, 
                    new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes)); 
                xmlSerializer.Serialize(xmlTextWriter, instance);
            }
            catch (Exception e)
            {
                rethrow = e;
            }
            finally
            {
                xmlTextWriter.Flush();
                xmlTextWriter.Close();
                if (xmlTextWriter.BaseStream != null)
                {
                    xmlTextWriter.BaseStream.Flush();
                    xmlTextWriter.BaseStream.Close();
                }
                if (rethrow != null)
                {
                    throw rethrow;
                }

            }



            /*
            FileStream stream = new FileStream(file.FullName, FileMode.Create);
            XmlSerializer serializer = new XmlSerializer(instance.GetType(), defaultNameSpace);
            serializer.Serialize(stream, instance);
            stream.Close(); 
            */
        }

        /// <summary>
        /// Deserializes an XML document instance to a instance of a class type
        /// </summary>
        /// <param name="type">The Type to be instantiated</param>
        /// <param name="file">The file</param>
        /// <param name="defaultNameSpace">The default namespace for the XML file</param>
        /// <returns></returns>
        public Object DeserializeFromFile(Type type, FileInfo file, string defaultNameSpace)
        {
            /*
            XmlSerializer xmlSerializer = new XmlSerializer(type, defaultNameSpace);
            XmlTextReader xmlTextReader = new XmlTextReader(
            Object instance = xmlSerializer.Serialize(xmlTextReader, instance);
            xmlTextReader.Close();
             */

            XmlSerializer serializer = new XmlSerializer(type, defaultNameSpace);
            FileStream stream = new FileStream(file.FullName, FileMode.Open);
            Object instance = serializer.Deserialize(stream);
            stream.Close();

            return instance;
        }


        /*
        public String SerializeObject(cocdType cocd) 
        {
            string XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(cocd.GetType());
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, cocd);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
         }

        public cocdType DeserializeObject(string xmlizedString)
        {
            XmlSerializer xs = new XmlSerializer(typeof(cocdType));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (cocdType)xs.Deserialize(memoryStream);
        }

        private String UTF8ByteArrayToString(Byte[] characters) 
        { 

            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }
 
        private Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        } 
        */
    }
}
